package com.dj.mingkong.portal.biz.controller.music;

import com.dj.mingkong.common.annotation.HasRoles;
import com.dj.mingkong.common.annotation.RequestLimit;
import com.dj.mingkong.common.enums.RoleEnum;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicAddVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicQueryVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.MusicUpdateVo;
import com.dj.mingkong.portal.biz.controller.music.vo.response.MusicVo;
import com.dj.mingkong.portal.biz.service.music.MusicService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 音乐管理
 *
 * @author zenglonghui
 * @date: 2022/8/20 19:59
 */
@RestController
@RequestMapping("/music")
@HasRoles(RoleEnum.MUSICIAN)
public class MusicController {
    @Resource
    private MusicService musicService;

    /**
     * 分页查询我的音乐
     *
     * @param queryVo {}
     * @return MusicVo
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<MusicVo>> pageByVo(@RequestBody @Validated MusicQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        return RestResponse.success(musicService.pageByVo(queryVo));
    }

    /**
     * 上传音乐
     *
     * @param addVo {}
     * @return Boolean
     */
    @PostMapping("/add")
    @RequestLimit(count = 2)
    public RestResponse<Boolean> insert(@RequestBody @Validated MusicAddVo addVo) {
        musicService.insert(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 音乐修改
     *
     * @param updateVo {}
     * @return Boolean
     */
    @PostMapping("/updateById")
    @RequestLimit(count = 2)
    public RestResponse<Boolean> updateById(@RequestBody @Validated MusicUpdateVo updateVo) {
        musicService.updateById(updateVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 分页搜索
     *
     * @param queryVo {}
     * @return MusicVo
     */
    /*@PostMapping("/searchByVo")
    public RestResponse<PageVo<MusicVo>> searchByVo(
            @RequestBody @Validated MusicQueryEsVo queryVo) {
        PageVo<MusicVo> pageVo = new PageVo<>();
        AssertUtils.checkQueryPageVo(queryVo);
        final SearchHits<MusicEsEo> searchHits = musicService.searchByVo(queryVo);
        pageVo.setTotal(searchHits.getTotalHits());
        final List<MusicVo> musicVoList = searchHits.getSearchHits().stream().map(searchHit ->
                SmartBeanCopy.copy(searchHit.getContent(), MusicVo.class)
                        .setHighlightName(searchHit.getHighlightField("name").get(0))).toList();
        pageVo.setRecords(musicVoList);
        return RestResponse.success(pageVo);
    }*/

    /**
     * 模糊搜索建议数据
     *
     * @param name String
     * @return String
     */
   /* @GetMapping("/searchLikeName/{name}")
    public RestResponse<List<String>> searchLikeName(@PathVariable(value = "name") String name) {
        if (StringUtils.isBlank(name)) {
            return RestResponse.success(Lists.newArrayList());
        }
        final SearchHits<MusicEsEo> searchHits = musicService.searchLikeName(name);
        final List<String> strings = searchHits.getSearchHits().stream().map(searchHit -> searchHit.getContent().getName()).toList();
        return RestResponse.success(strings);
    }*/
}
